#include "entitydialog.h"
#include "ui_entitydialog.h"
#include "levelpainter.h"
#include <QMessageBox>
#include <QFileDialog>

EntityDialog::EntityDialog(QWidget *parent) : QDialog(parent),
    ui(new Ui::EntityDialog)
{
    ui->setupUi(this);

    connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(ButtonBoxPressed(QAbstractButton*)));
    connect(this, SIGNAL(ResetFields()), this, SLOT(ResetValues()));
    connect(this, SIGNAL(SaveChanges()), this, SLOT(SaveValues()));
    connect(this, SIGNAL(SaveChanges()), this, SLOT(CloseDialog()));
    connect(this, SIGNAL(Discard()), this, SLOT(CloseDialog()));
    connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(DeleteEntity()));

    connect(ui->hasScript,    SIGNAL(toggled(bool)), this, SLOT(EnableScript(bool)));
    connect(ui->searchScript, SIGNAL(clicked()),     this, SLOT(SearchScript()));
}

EntityDialog::~EntityDialog()
{
    delete ui;
}

void EntityDialog::EnableScript(bool is)
{
    ui->groupScript->setEnabled(is);
    _object->SetHasScript(is);
}

QString GetNameFromPath(const QString& filepath);

void EntityDialog::SearchScript()
{
    QString scriptPath;

    scriptPath = QFileDialog::getOpenFileName(this, "Picking Script");
    if (scriptPath != "")
    {
        scriptPath = GetNameFromPath(scriptPath);
        ui->scriptSource->setText(scriptPath);
        _object->SetScriptSource(scriptPath);
        _object->SetScriptType("cpp");
    }
}

void EntityDialog::ButtonBoxPressed(QAbstractButton* button)
{
    QDialogButtonBox::ButtonRole role = ui->buttonBox->buttonRole(button);

    switch (role)
    {
    case QDialogButtonBox::ResetRole:
        ResetFields();
        break ;
    case QDialogButtonBox::AcceptRole:
        SaveChanges();
        break ;
    case QDialogButtonBox::RejectRole:
    case QDialogButtonBox::DestructiveRole:
        Discard();
        break ;
    default:
	break ;
    }
}

void EntityDialog::SetLPObject(LPObject* object)
{
    this->setWindowTitle("Edition de '" + object->EntityName() + "'");
    _object = object;
    ResetValues();
}

void EntityDialog::ResetValues()
{
    sf::Vector2f pos(_object->GetPosition());

    ui->positionX->setValue(pos.x);
    ui->positionY->setValue(pos.y);
    ui->rotation->setValue(_object->GetRotation());
    ui->isLocked->setChecked(_object->PositionLocked());
    ui->name->setText(_object->EntityName());
    ui->hasScript->setChecked(_object->HasScript());
    ui->scriptSource->setText(_object->ScriptSource());
}

void EntityDialog::SaveValues()
{
    sf::Vector2f pos;

    pos.x = ui->positionX->value();
    pos.y = ui->positionY->value();
    _object->SetPosition(pos);
    _object->SetRotation(ui->rotation->value());
    _object->SetPositionLocked(ui->isLocked->isChecked());
    _object->SetEntityName(ui->name->text());
}

void EntityDialog::CloseDialog()
{
    this->close();
}

void EntityDialog::DeleteEntity()
{
    int ret = QMessageBox::warning(this, "Supprimer ?", "Non ! Pas moi ! J'ai une femme est des enfants !", QMessageBox::Yes, QMessageBox::No);

    if (ret == QMessageBox::Yes)
    {
        DeleteEntity(_object->EntityName());
        CloseDialog();
    }
}
